package com.yurun.controller.admin;

import com.yurun.entity.Article;
import com.yurun.model.vo.CategoryArticleCount;
import com.yurun.model.vo.PageResult;
import com.yurun.model.vo.Result;
import com.yurun.model.vo.article.ArticleInfo;
import com.yurun.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class ArticleAdminController {
    @Autowired
    ArticleService articleService;
    /**
     * @Description: 后台-添加文章
     * @Param: article
     * @return: void
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @PostMapping("/article")
    public void addArticle(@RequestBody Article article){
        articleService.addArticle(article);
    }
    /**
     * @Description: 后台-通过ID-删除文章
     * @Param: id
     * @return: void
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @DeleteMapping("/article/id")
    public void deleteArticleById(@RequestParam Long id){
        articleService.deleteArticleById(id);
    }
    /**
     * @Description: 后台-通过id-更新置顶文章
     * @Param: id
     * @Param isTop
     * @return: void
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @PutMapping("/articletop/id")
    public void updateArticleTopById(@RequestParam Long id,
                                     @RequestParam Boolean isTop){
        articleService.updateArticleTopById(id, isTop);
    }
    /**
     * @Description: 后台-通过ID-更新文章浏览量
     * @Param: id
     * @Param views
     * @return: void
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @PutMapping("/articleviews/id")
    public void updateArticleViewsById(@RequestParam Long id,
                                       @RequestParam Integer views){
        articleService.updateArticleViewsById(id, views);
    }
    /**
     * @Description: 后台-更新文章信息
     * @Param: article
     * @return: void
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @PutMapping("/article")
    public void updateArticle(@RequestBody Article article){
        articleService.updateArticle(article);
    }
    /**
     * @Description: 后台-获取文章列表
     * @Param: pageNum
     * @return: com.yurun.model.vo.Result
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @GetMapping("/articleinfolist")
    public Result getAllArticleInfoList(@RequestParam(defaultValue = "1") Integer pageNum){
        PageResult<ArticleInfo> object=articleService.getAllArticleInfoList(pageNum);
        return Result.ok("获取文章简要信息列表成功",object);
    }
    /**
     * @Description: 后台-通过分类-获取文章列表
     * @Param: category_id
     * @Param pageNum
     * @return: com.yurun.model.vo.Result
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @GetMapping("/articleinfolist/categoryid")
    public Result getArticleInfoListByCategory(@RequestParam Integer category_id,
                                               @RequestParam(defaultValue = "1") Integer pageNum){
        PageResult<ArticleInfo> object=articleService.getArticleInfoListByCategory(category_id,pageNum);
        return Result.ok("通过分类id获取文章简要信息列表成功",object);
    }
    /**
     * @Description: 后台-通过标题-获取文章列表
     * @Param: title
     * @Param pageNum
     * @return: com.yurun.model.vo.Result
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @GetMapping("/articleinfolist/title")
    public Result getArticleInfoListByTitle(@RequestParam String title,
                                            @RequestParam(defaultValue = "1") Integer pageNum){
        PageResult<ArticleInfo> object=articleService.getArticleInfoListByTitle(title,pageNum);
        return Result.ok("通过标题获取文章简要信息列表成功",object);
    }
    /**
     * @Description: 后台-获取年月列表
     * @Param:
     * @return: com.yurun.model.vo.Result
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @GetMapping("/groupyearmonthlist")
    public Result getGroupYearMonthList(){
        List<String> object=articleService.getGroupYearMonthList();
        return Result.ok("获取年月列表成功",object);
    }
    /**
     * @Description: 后台-通过ID-获取文章的详细信息
     * @Param: id
     * @return: com.yurun.model.vo.Result
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @GetMapping("article/id")
    public Result getArticleById(@RequestParam Integer id){
        Article object=articleService.getArticleById(id);
        return Result.ok("获取文章信息成功",object);
    }
    /**
     * @Description: 后台-获取文章数量
     * @Param:
     * @return: com.yurun.model.vo.Result
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @GetMapping("/countofarticle")
    public Result getCountOfArticle(){
        int object=articleService.getCountOfArticle();
        return Result.ok("获取文章数量成功",object);
    }
    /**
     * @Description: 后台-通过分类-获取该分类下文章数量
     * @Param: categoryId
     * @return: com.yurun.model.vo.Result
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @GetMapping("/countofarticle/categoryid")
    public Result getCountOfArticleByCategoryId(@RequestParam Integer categoryId){
        int object=articleService.getCountOfArticleByCategoryId(categoryId);
        return Result.ok("通过分类id获取文章数量成功",object);
    }
    /**
     * @Description: 后台-通过分类-获取每个分类下的文章数量
     * @Param:
     * @return: com.yurun.model.vo.Result
     * @Author: JimmyYao
     * @Date: 2022/2/9
     */
    @GetMapping("/countlistofarticleaboutcategory")
    public Result getCountListOfArticleAboutCategory(){
        List<CategoryArticleCount> object=articleService.getCountListOfArticleAboutCategory();
        return Result.ok("获取每个类别下文章的数量成功",object);
    }
}
